define(function(require,exports,module){
	require('uploadify');
	//关于我们管理
	var about_us = require("module/backend/aboutus.js");
	//加盟管理
	var join = require("module/backend/join.js");
	//商品管理
	var food = require("module/backend/food.js");
	//用户管理
	var user = require("module/backend/user.js");
	exports.init = function(){
		about_us.init();
		join.init();
		food.init();
		user.init();
		//预览菜谱图片
		preview_cookbook_img();
		//点击左侧栏出现右侧栏
		click_to_show_right();
		//点击卡槽出现对应的div
		click_li_to_show_div();
		//上传菜谱的图片
		upload_cookfood_img();
	    //实例化编辑器
	    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
	    UE.getEditor('editor');
	    
	}	
	exports.operate = function(){
		about_us.operate();
		join.operate();
		food.operate();
		user.operate();
		//cookbook添加点击确认
		cookbook_form_click();
		//管理员退出登陆
		admin_login_out();
		
		//cookbook  page_show_click   ajax翻页
		cook_book_page_link_click()

		//cookbook  删除 
		cookbook_single_del();
		//cookbook  多项删除
		cookbook_mutil_del();

		//cookbook  禁用
		cookbook_forbidden();
		//cookbook  多项禁用
		cookbook_mutil_forbidden();
		//cookbook 恢复
		cookbook_recover();
		//cookbook  多项恢复
		cookbook_mutil_recover();

		//编辑菜谱
		cookbook_edit();
	}
	exports.win_resize = function(){

	}
	function preview_cookbook_img(){
		$("img[name='show_cookbook_img']").fancybox({
			aspectRatio:true,
			autoResize:true,
			closeClick:true,
			title:'图片若是太大，请拖动滚动条查看，有待改善！请谅解'
		});
		$(".click_show_cbpic").live('click',function(){
			$("img[name='show_cookbook_img']").trigger('click');
		})	

	}
	function click_li_to_show_div(){
		$(".main_card_content_ul li").live('click',function(){
			var div_num = $(this).attr('target-div');
			$(this).parent().find('li').removeClass('action-target-li');
			$(this).addClass("action-target-li");
			$(this).parents(".main_card").find(".target-div div").removeClass('target-div-active');
			$(this).parents(".main_card").find(".target-div .target-div-"+div_num).addClass('target-div-active');
		});
	}

	function click_to_show_right(){
		$(".left_ul li").live('click',function(){
			$(this).parent().find("li").removeClass('left_li_active');
			$(this).addClass('left_li_active');
			var point_div = $(this).attr('pointer-div');
			$(".main_card").removeClass('main_card_active');
			$(".right_content ."+point_div).addClass("main_card_active");
		})
	}
	function upload_cookfood_img(){
	    $('#cookfood_img').uploadify({
	    	'fileTypeExts' : '*.gif; *.jpg; *.png',
	    	'buttonImage':backend_var.uploadify_buttonImg,
	        'swf'      : backend_var.uploadify_url,
	        'formData':{
	        	'asdfa':"adsasdf"
	        },
	        'itemTemplate':'',
	        'removeTimeout' : 100,
	        'uploader' : backend_var.ajaxUploadCookFoodImgUrl,
	        'onUploadSuccess' : function(file, e, response) {
           		var e = $.parseJSON(e);
           		$("input[name='cookbook_photo']").val(e.data['savename']);
           		$("img[name='show_cookbook_img']").attr("src",backend_var.upload_url+"/"+e.data['savepath']+"/"+e.data['savename']);
           		$(".click_show_cbpic").show();
				$.Zebra_Dialog('图片上传成功，请填写其他信息', {
				    'type':'information',
				    'title':'提示',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});          		
        	}
	    });
	}
	function cookbook_form_click(){
		$(".cookbook_button").live('click',function(){
			var data = $("form[name='cookbook_form']").serializeArray();
			$.post(backend_var.ajaxCookBookRegisterUrl,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'成功',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});	
				}
			},'json');
		})
	}
	function admin_login_out(){
		$(".admin_login_out a").live('click',function(){
			$.post(backend_var.adminLoginOutUrl,{},function(){
				$.Zebra_Dialog('退出成功', {
				    'type':'information',
				    'buttons':  ['确定'],
				    'onClose':  function(caption) {
						window.location = backend_var.login_to_url;	
				    }
				});					
			},'json');
		});
	}
	
	function cook_book_page_link_click(){
		$(".cookbook_page_show a").live('click',function(){
			var a_class = $(this).attr('class');
			var page_num;
			if(a_class == 'num'){
				page_num = $(this).text();
			}
			if(a_class == 'next'){
				page_num = Number($(".cookbook_page_show .current").text()) + 1;

			}
			if(a_class == 'prev'){
				page_num = $(".cookbook_page_show .current").text() - 1;
			}
			//这里的'p'参数是与page分页类中的   //分页参数名 一致的
			data = {p:page_num};
			$.get(backend_var.ajaxGetCookBookPageDataUrl,data,function(html){
				$(".cookbook_replace_div").html(html);
			},'html');
			return false;
		});
	}

	function cookbook_single_del(){
		$(".cookbook_delete").live('click',function(){
			var click_obj = $(this);
			var cookbook_id = $(this).parents('tr').attr("data-id");
			var data={
				'cookbook_id':cookbook_id
			};
			$.post(backend_var.ajaxDelCookbookUrl,data,function(e){
				if(e.status){
					click_obj.parents("tr[data-id='"+cookbook_id+"']").fadeOut(400);
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});	
				}	
			},'json');
		})
	}

	function cookbook_mutil_del(){
		//选中多个
		choose_all();
		//删除多个
		cookbook_del_all();
	}
	function choose_all(){
		$("input[name='cookbook_all']").live('click',function(){
			var checkbox_status = $(this).attr("checked");
			if(checkbox_status == 'checked'){
				$("input[name='cookbook_id[]']").attr('checked','checked');
			}else{
				$("input[name='cookbook_id[]']").removeAttr('checked');
			}
		});
	}
	function cookbook_del_all(){
		$(".cookbook_multi_delete").live('click',function(){
			var mutil_button_checked = $("input[name='cookbook_all']").attr("checked");
			if(mutil_button_checked == 'undefined'){
				return false;
			}else{
				var data = $("form[name='cookbook_mutil_operate']").serializeArray();
			
				$.post(backend_var.ajaxDelCookbookUrl,data,function(e){
					if(e.status){
						for (var i = e.data.length - 1; i >= 0; i--) {
							$("tr[data-id="+e.data[i]+"]").fadeOut(400);
						};	
					}else{
						$.Zebra_Dialog(e.message, {
						    'type':'error',
						    'buttons':  ['确定'],
						    'onClose':  function(caption) {	
						    }
						});	
					}	
				},'json');				
			}
		})
	}

	function cookbook_forbidden(){
		$(".cookbook_forbidden").live('click',function(){
			var click_obj = $(this);
			var cookbook_id = $(this).parents("tr").attr("data-id");
			var data = {
				cookbook_id:cookbook_id
			}
			$.post(backend_var.ajaxForbiddenCookbookUrl,data,function(e){
				if(e.status){
					click_obj.text("恢复");
					click_obj.removeClass("cookbook_forbidden").addClass("cookbook_forbidden_on");
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});	
				}	
			},'json');			
		})
	}

	function cookbook_mutil_forbidden(){
		$(".cookbook_multi_forbidden").live('click',function(){
			var mutil_button_checked = $("input[name='cookbook_all']").attr("checked");
			if(mutil_button_checked == 'undefined'){
				return false;
			}else{
				var data = $("form[name='cookbook_mutil_operate']").serializeArray();
			
				$.post(backend_var.ajaxForbiddenCookbookUrl,data,function(e){
					if(e.status){
						for (var i = e.data.length - 1; i >= 0; i--) {
							$("tr[data-id="+e.data[i]+"]").find(".cookbook_forbidden").text("恢复").removeClass("cookbook_forbidden").addClass("cookbook_forbidden_on");
						};	
					}else{
						$.Zebra_Dialog(e.message, {
						    'type':'error',
						    'buttons':  ['确定'],
						    'onClose':  function(caption) {	
						    }
						});	
					}	
				},'json');				
			}
		})		
	}

	function cookbook_recover(){
		$(".cookbook_forbidden_on").live('click',function(){
			var click_obj = $(this);
			var cookbook_id = $(this).parents("tr").attr("data-id");
			var data = {
				cookbook_id:cookbook_id
			}
			$.post(backend_var.ajaxRecoverCookbookUrl,data,function(e){
				if(e.status){
					click_obj.text("禁用");
					click_obj.removeClass("cookbook_forbidden_on").addClass("cookbook_forbidden");
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'buttons':  ['确定'],
					    'onClose':  function(caption) {	
					    }
					});	
				}	
			},'json');			
		})
	}

	function cookbook_mutil_recover(){
		$(".cookbook_multi_recover").live('click',function(){
			var mutil_button_checked = $("input[name='cookbook_all']").attr("checked");
			if(mutil_button_checked == 'undefined'){
				return false;
			}else{
				var data = $("form[name='cookbook_mutil_operate']").serializeArray();
			
				$.post(backend_var.ajaxRecoverCookbookUrl,data,function(e){
					if(e.status){
						for (var i = e.data.length - 1; i >= 0; i--) {
							$("tr[data-id="+e.data[i]+"]").find(".cookbook_forbidden_on").text("禁用").removeClass("cookbook_forbidden_on").addClass("cookbook_forbidden");
						};	
					}else{
						$.Zebra_Dialog(e.message, {
						    'type':'error',
						    'buttons':  ['确定'],
						    'onClose':  function(caption) {	
						    }
						});	
					}	
				},'json');				
			}
		})		
	}

	function cookbook_edit(){
		$(".cookbook_edit").live('click',function(){
			var data = {
				cookbook_id:$(this).parents("tr").attr('data-id')
			};
			$.post(backend_var.ajaxGetCookbookEditPageUrl,data,function(html){
				var cookbook_edit_div = $($(".cookbook_eidt_div"));
				//选中编辑菜谱这个选项卡
				cookbook_edit_div.parents(".main_card_content").find("ul li").removeClass("action-target-li");	
				cookbook_edit_div.parents(".main_card_content").find("ul li[target-div=3]").show().addClass('action-target-li');

				//选中编辑菜谱这个选项卡对应的div
				cookbook_edit_div.parents(".main_card_content").find(".target-div .target-son").removeClass('target-div-active');
				cookbook_edit_div.parents(".main_card_content").find(".target-div .target-div-3").addClass('target-div-active');

				//将编辑表单取回，填充在相应的位置
				cookbook_edit_div.html(html);
				UE.getEditor('edit_editor');

				//预览图片
				preview_cookbook_edit_img();
				//更新图片
				cookbook_edit_upload_img();	
				//点击保存
				cookbook_edit_click_button();

			},'html');
		})
	}

	function cookbook_edit_upload_img(){
		$('#cookbook_img').uploadify({
	    	'fileTypeExts' : '*.gif; *.jpg; *.png',
	    	'buttonImage':backend_var.uploadify_buttonImg,
	        'swf'      : backend_var.uploadify_url,
	        'formData':{
	        	'asdfa':"adsasdf"
	        },
	        'itemTemplate':'',
	        'removeTimeout' : 100,
	        'uploader' : backend_var.ajaxUploadCookFoodImgUrl,
	        'onUploadSuccess' : function(file, e, response) {
           		var e = $.parseJSON(e);
           		$("input[name='cookbook_eidt_photo']").val(e.data['savename']);
           		$("img[name='show_cookbook_eidt_img']").attr("src",backend_var.upload_url+"/"+e.data['savepath']+"/"+e.data['savename']);
				$.Zebra_Dialog('图片上传成功，请填写其他信息', {
				    'type':'information',
				    'title':'提示',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});		
        	}
	    });			
	}
	function preview_cookbook_edit_img(){
		$("img[name='show_cookbook_eidt_img']").fancybox({
			aspectRatio:true,
			autoResize:true,
			closeClick:true,
			title:'图片若是太大，请拖动滚动条查看，有待改善！请谅解'
		});
		$(".click_show_edit_cbpic").live('click',function(){
			$("img[name='show_cookbook_eidt_img']").trigger('click');
		})	
	}

	function cookbook_edit_click_button(){
		$(".cookbook_eidt_button").live('click',function(){
			var data = $("form[name='cookbook_eidt_form']").serializeArray();
			$.post(backend_var.ajaxSaveCookcookEditUrl,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});						
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});						
				}
			},'json');				
		});
	}
});